<?php

namespace App\Http\Controllers\Laravel\ORM;

use App\BCAM\CMVariable\CMVariable;
use App\Model\Account\User;
use App\Tools\Controller\ControllerMethodRoute;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;


class ModelController extends Controller
{
    use ControllerMethodRoute;
    public $home = '/laravel/orm/model/';
    public function ormInsert(){
        $ormM = new User();
        $ormM->name  =  '测试'.date('Y-m-d H:i:s');
        $ormM->email = date('Y-m-d H:i:s').'@12.com';
        $ormM->password = '123@12.com';
        $res = $ormM->save();
        //$ormM是那条记录实例,并且插入表格时,可以用,$res是bool值
        CMVariable::compareRes($ormM,$res);
    }
    //
    public function ormUpdate(){
        //http://blog.csdn.net/sanbingyutuoniao123/article/details/54138065
        //Laravel 5 中需要开启QueryLog
        \DB::connection()->enableQueryLog();
        \DB::beginTransaction();
        //$ormM = User::first(1);//不能这样用
        $ormM = User::find(1);//是这样用
        sleep(10);
        $ormM ->update(['name'=>'242']);
        \DB::commit();
        CMVariable::compareRes($ormM,\DB::getQueryLog());
        //CMVariable::compareRes($ormM);
    }

    public function ormFindOrFail(){
        try{
            User::where('name','1506577157')->FindOrFail(1);
        }catch(ModelNotFoundException $e){
            var_dump($e->getMessage());
        }

    }


}
